#ifndef NETLIB_POLLER_H
#define NETLIB_POLLER_H

// IO复用模型的抽象类

#include <map>
#include <vector>

#include "Timestamp.h"
#include "EventLoop.h"

namespace netlib
{

namespace net
{

class Channel;

class Poller : noncopyable
{
public:
    typedef std::vector<Channel*> ChannelList;

    Poller(EventLoop* loop);

    virtual ~Poller();

    virtual Timestamp poll(int timeoutMs, ChannelList* activeChannels) = 0;

    virtual void updateChannel(Channel* channel) = 0;

    virtual void removeChannel(Channel* channel) = 0;

    virtual bool hasChannel(Channel* channel) const; // 判断该通道是否在poll参数列表中

    static Poller* newDefaultPoller(EventLoop* loop);

    void assertInLoopThread() const // 断言是否为创建者调用，否则退出程序
    { ownerLoop_->assertInLoopThread(); }

protected:
    typedef std::map<int, Channel*> ChannelMap;
    ChannelMap channels_;                           // key：fd，value：Channel*，监听表

private:
    EventLoop* ownerLoop_;                          // 该对象所属的EventLoop对象

}; // class Poller end

} // namespace net end

} // namespace netlib end

#endif  // netlib_NET_POLLER_H
